package #(table.packageName).controller;

import #(table.packageName).domain.#(table.name);
#if(option.isGenFormVo())
import #(table.packageName).domain.#(table.name)Vo;
#end

import org.noear.solon.core.handle.Result;
import com.palm.core.data.PageResult;
import com.palm.core.anno.Auth;
import org.noear.solon.annotation.*;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.Page;
import org.noear.solon.extend.sqltoy.annotation.Db;

#if(option.isGenExport())
import com.palm.boot.util.TransportUtil;
#end
#if(option.isGenPriv())
import #(table.packageName).#(firstToUpperCase(table.module))Menu;
#end
#if(!option.isGenMapper())
import org.sagacity.sqltoy.config.model.Translate;
#end

import java.util.List;

/**
* #(table.title)
*/
@Auth
@Controller
@Mapping("#(firstToLowerCase(table.name))")
public class #(table.name)Controller{

    @Db
    SqlToyLazyDao dao;

    /**
    * 新增#(table.title)
    */
    #if(option.isGenPriv())
    @Auth(#(firstToUpperCase(table.module))Menu.#(table.name.toUpperCase())_ADD)
    #end
    @Mapping("add")
    public Result add(#(table.name) #(firstToLowerCase(table.name))){
        dao.save(#(firstToLowerCase(table.name)));
        return Result.succeed();
    }

    /**
    * 删除#(table.title)
    */
    #if(option.isGenPriv())
    @Auth(#(firstToUpperCase(table.module))Menu.#(table.name.toUpperCase())_DELETE)
    #end
    @Mapping("delete")
    public Result delete(List<#(table.name)> #(firstToLowerCase(table.name))s){
        // dao.loadAll(#(firstToLowerCase(table.name))s);
        dao.deleteAll(#(firstToLowerCase(table.name))s);
        return Result.succeed();
    }

    /**
    * 更新#(table.title)
    */
    #if(option.isGenPriv())
    @Auth(#(firstToUpperCase(table.module))Menu.#(table.name.toUpperCase())_EDIT)
    #end
    @Mapping("save")
    public Result save(#(table.name) #(firstToLowerCase(table.name))){
        dao.update(#(firstToLowerCase(table.name)));
        return Result.succeed();
    }

    /**
    *  获取#(table.title)
    */
    #if(option.isGenPriv())
    @Auth(#(firstToUpperCase(table.module))Menu.#(table.name.toUpperCase()))
    #end
    @Mapping("detail")
    public Result<#(table.name)> detail(#(table.name) query){
       #(table.name) #(firstToLowerCase(table.name))=dao.load(query);
       return Result.succeed(#(firstToLowerCase(table.name)));
    }
    /**
    * 获取#(table.title)列表
    */
    #if(option.isGenPriv())
    @Auth(#(firstToUpperCase(table.module))Menu.#(table.name.toUpperCase()))
    #end
    @Mapping("list")
    public Result<List<#(table.name)#if(option.isGenFormVo())Vo#end>> list(#(table.name)#if(option.isGenFormVo())Vo#end  query, Page page){
        #if(option.isGenMapper())
        Page<#(table.name)#if(option.isGenFormVo())Vo#end> result = dao.findPageBySql(page,"#(firstToLowerCase(table.name))_list",query);
        #else
        String sql="SELECT #(table.listFields) FROM #(table.code) WHERE #for(column:table.columns)#if(column.isSearchCondition())#[ and #(column.code) = :#(column.field)]#end#end";
        Page<#(table.name)#if(option.isGenFormVo())Vo#end> result = dao.findPageBySql(page,sql,query);
        #end
        return PageResult.succeed(result);
    }

    #if(option.isGenExport())
    /**
    * 导出#(table.title)
    */
    #if(option.isGenPriv())
    @Auth(#(firstToUpperCase(table.module))Menu.#(table.name.toUpperCase()))
    #end
    @Mapping("export")
    public void export(#(table.name)#if(option.isGenFormVo())Vo#end  query, Context  ctx){
        #if(option.isGenMapper())
        TransportUtil.exportExcel(dao,"#(firstToLowerCase(table.name))_list",query,"#(table.title)");
        #else
        String sql="SELECT #(table.listFields) FROM #(table.code) WHERE #for(column:table.columns)#if(column.isSearchCondition())#[ and #(column.code) = :#(column.field)]#end#end";
        TransportUtil.exportExcel(dao,sql,query,"#(table.title)");
        #end
    }
    #end
}
